import { anewPay, orderCancel, orderList } from "../../../api/order"
import { shareAppMessageSet } from "../../../utils/config"
import { dataCollection } from "../../../utils/dataCollection"
import { listProps } from "../../../utils/type"

Page({

  /**
   * 页面的初始数据
   */
  data: {
    orderData: <listProps>{
      list: [],
      current: 1,
      total: 0
    },
    orderStatus: <string[]>['待支付', '已支付', '已取消', '已关闭', '退款中', '已退款'],//订单状态(0.待支付；1.已支付；2.已取消；3.已关闭；4.退款中；5.已退款)
    refresherTriggered: false,
    themeCode:''
  },
  page: {
    current: 1,
    total: 0,
  },
  /**
   * 生命周期函数--监听页面加载
   */
  onLoad() {
     //从本地获取当前选择的城市编码
     this.setData({
      themeCode: wx.getStorageSync('themeCode')
    })
  },
  //获取订单列表
  getOrderList(callback = () => { }) {
    orderList(this.data.orderData.current).then(res => {
      const list: any[] = this.data.orderData.list
      if (this.data.orderData.current === 1) {
        list.length = 0
      }
      list.push(...res.result.records)
      let index = this.data.orderData.current
      index++
      const obj: listProps = {
        current: index,
        list: [...list],
        total: res.result.total
      }
      this.setData({
        orderData: obj
      })
      callback()
    })
  },
  //取消支付
  onCancelPay(e: any) {
    console.log(e)
    wx.showModal({
      title: '确认取消支付吗？',
      success: (res: any) => {
        if (res.confirm) {
          orderCancel(e.currentTarget.dataset.orderNo).then(r => {
            this.setData({
              'orderData.current': 1
            })
            this.getOrderList()
          })
        }
      }
    })
  },
  //去支付
  onGotoPay(e: any) {
    anewPay(wx.getStorageSync('userInfo').openId, e.currentTarget.dataset.orderId).then(res => {
      wx.requestPayment({
        timeStamp: res.result.wxOrderResult.timeStamp,
        nonceStr: res.result.wxOrderResult.nonceStr,
        package: res.result.wxOrderResult.packageValue,
        signType: res.result.wxOrderResult.signType,
        paySign: res.result.wxOrderResult.paySign,
        success: (e: any) => {
          console.log('支付成功', e)
          this.setData({
            'orderData.current': 1
          })
          this.getOrderList()
        },
        fail: (e: any) => { }
      })
    })
  },
  //页面触底触发
  onReachBottom() {
    console.log('onReachBottom')
    if (this.data.orderData.list.length < Number(this.data.orderData.total)) {
      this.getOrderList()
    }
  },
  bindrefresherpulling(res: any) {
    console.log('bindrefresherpulling', res)
  },
  bindrefresherrefresh(res: any) {
    console.log('bindrefresherrefresh', res)
    this.setData({
      refresherTriggered: true,
      'orderData.current': 1
    })
    this.getOrderList(() => {
      this.setData({
        refresherTriggered: false
      })
    })
  },
  bindrefresherrestore(res: any) {
    console.log('bindrefresherrestore', res)
  },
  bindrefresherabort(res: any) {
    console.log('bindrefresherabort', res)
  },
  bindscrolltolower(res: any) {
    console.log('bindscrolltolower', res)
    if (this.data.orderData.list.length < Number(this.data.orderData.total)) {
      this.getOrderList()
    }
  },
  /**
   * 用户点击右上角分享
   */
  onShareAppMessage() {
    return { ...shareAppMessageSet() }
  },
  //复制订单编号
  onCopyOrderNo(e:any) {
    wx.setClipboardData({
      data:e.currentTarget.dataset.orderNo,
      success(res) {
        wx.getClipboardData({
          success(res) {
            console.log(res.data) // data
          }
        })
      }
    })
  },
  onHide() {
    dataCollection.setPageMapData('leave')
  },
  onUnload() {
    dataCollection.setPageMapData('leave')
  },
  onShow() {
    dataCollection.setPageMapData('enter', '订单列表')
    this.getOrderList()
  },
})